#-------------------------------------------------------------------------------
# Name:        module1
# Purpose:
#
# Author:      Student
#
# Created:     04/07/2012
# Copyright:   (c) Student 2012
# Licence:     <your licence>
#-------------------------------------------------------------------------------
import random
import codecs
import sys
##wordlist = open ("C:/Users/Student/Desktop/Daniel's stuff/wordlist.txt")
wordlist=[]
f=open('C:/Users/Student/Desktop/Daniel_stuff/wordlist.txt', 'r')
c=0
for x in f:
        #weird replacement due to encoding
        x=repr(x).replace('x00','')
        x=repr(x).replace('\\','')
        x=repr(x).replace('\'','')
        if(len(x)>6 and c>0):
            wordlist.append(x[2:len(x)-3])
        c=c+1
strikes = 0
hidden_letters = []
alphabet = "abcdefghijklmnopqrstuvwxyz"
guesses = []

def new_word():
    global word
    global guesses
    word = random.choice(wordlist)
    for letter in set(word):
        if letter in alphabet:
            hidden_letters.append(letter)
    guesses = []

def current_progress():
    for letter in word:
        if letter in hidden_letters:
            print ('_'),

        elif letter == ' ':
            print (' '),

        else:
            print (letter),
    print ("\n")

def play_again():
    global strikes
    print ("Would you like to play again?")
    answer = input("y/n: ")
    if answer == "y":
        strikes = 0
        main()
    elif answer == "n": exit(0)
    else:
        print ("That's not a valid answer.")
        play_again()

def letter_in_word(x):
    global strikes
    global hidden_letters
    if x in word:
        hidden_letters.remove(x)
        print ("That letter is in the word.")
        current_progress()
        if hidden_letters == []:
            print ("You win!")
            play_again()
        else:
            print ("You have %d strike(s)." % strikes)

    elif not x in word:
        print ("That letter is not in the word.")
        current_progress()
        strikes = strikes + 1
        if(strikes>=1):
            print("   (O O)   ")
        if(strikes>=2):
            print("    \ /    ")
        if(strikes>=3):
            print("     |     ")
        if(strikes>=4):
            print("    / \    ")
        if(strikes>=5):
            print(" Oh no!!!!!")
        print ("You have %d strike(s)." % strikes)

def main():
    new_word()
    current_progress()
    global strikes
    while strikes < 6 and not hidden_letters == []:
        print ("Guess a letter. \n Letters that have been already guessed are:", guesses)

        guess = input("> ")

        if guess in alphabet and len(guess) == 1:
            if not guess in guesses:
                guesses.append(guess)
                letter_in_word(guess)

            else:
                print ("You've already guessed that letter. Pick another.")
                current_progress()
                print ("You have %d strikes." % strikes)

        else:
            print ("Sorry, that's not a valid guess.")
            current_progress()
            print ("You have %d strikes." % strikes)

    if strikes == 6:
        print ("Oops! You lose.")
        print ("The answer was:", word)
        play_again()

print ("Welcome to Hangman!")
print ("Six strikes and you lose.")
print ("----------")



if __name__ == '__main__':
    main()
